<?php

class ThreadsController extends \Phalcon\Mvc\Controller
{

	public static function upload($unique_id,$from,$height,$width)
    {
        
        return \Cloudinary\Uploader::upload($from,
        array(
           "public_id" => $unique_id,
           "height" => $height,
           "width" => $width,
           "invalidate" => true
        ))['version'];

    }

    public function indexAction()
    {

    }

    public function uploadAction()
    {
    	$forum_id = $this->request->getPost('forum_id');
		$image1 = $this->request->getPost('image1');
		$image2 = $this->request->getPost('image2');
		$image3 = $this->request->getPost('image3');
		$pictures = array();

		$forum = Threads::findFirstById($forum_id);
	
		if($image1)
		{
			$up = $this->upload('forum_pictures_'.$forum_id,$image1,PROF_HEIGHT,PROF_WIDTH);
			if(!$up)
            {
                $result['status'] = false;
                echo json_encode($result);
		    	$this->view->disable();
		    	exit();
            }
            $pictures['image1'] = $up.'/forum_pictures_'.$forum_id.'.jpg';
		}

		if($image2)
		{
			$up = $this->upload('forum_pictures_'.$forum_id,$image2,PROF_HEIGHT,PROF_WIDTH);
			if(!$up)
            {
                $result['status'] = false;
                echo json_encode($result);
		    	$this->view->disable();
		    	exit();
            }
            $pictures['image2'] = $up.'/forum_pictures_'.$forum_id.'.jpg';
		}
		if($image3)
		{
			$up = $this->upload('forum_pictures_'.$forum_id,$image3,PROF_HEIGHT,PROF_WIDTH);
			if(!$up)
            {
                $result['status'] = false;
                echo json_encode($result);
		    	$this->view->disable();
		    	exit();
            }
            $pictures['image3'] = $up.'/forum_pictures_'.$forum_id.'.jpg';
		}
		$forum->pictures = json_encode($pictures);
		if(!$forum->save())
		{
			    $result['status'] = false;
		        echo json_encode($result);
		    	$this->view->disable();
		    	exit();
		}

	    $result = array();
    	$result['status'] = true;

        $data = Threads::listThreadsInfo($forum_id);


        foreach ($data as $value) {

        $total_pictures = 0;
        $pictures = json_decode($value->pictures);
        if($pictures->image1) $total_pictures++;
        if($pictures->image2) $total_pictures++;
        if($pictures->image3) $total_pictures++;
        $status = ($value->status) ? "Active" : "Inactive";

            $push_data = array(
                  'id' => $value->id, 
                  'title' => $value->title,
                  'tags' => $value->tags,
                  'content' => $value->content,
                  'user_id' => $value->user_id,
                  'views' => $value->views,
                  'last_reply' => $value->last_reply,
                  'date_created' => $value->date_created,
                  'category' => $value->category,
                  'status' => $value->status,
                  'pictures' => $value->pictures,
                  'username' => $value->username,
                  'total_pictures' => $total_pictures,
                  'status'=>$status
                );
        }
        $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
        $pusher->trigger('my-channel', 'added-forum', $push_data);
    	
        echo json_encode($result);
    	$this->view->disable();


    }


    public function createAction()
    {
    	$result = array();


    	$data = array(
    		'title' => $this->request->getPost('title'),
			'category' => $this->request->getPost('category'),
			'content' => $this->request->getPost('content'),
			'user_id' => $this->session->get('user_id')
    		);

    	// upload image1
    	$thread = new Threads();
    	if(!$thread->save($data)){
			$result['messages'] = "Something went wrong! Please try again later";	
			$result['status'] = true;
	    	echo json_encode($result);
	    	$this->view->disable();    		
	    	exit();
    	}

    	$result['forum_id'] = $thread->id;
    	$result['status'] = true;
    	echo json_encode($result);
    	$this->view->disable();
    }

    public function replyAction()
    {
      $result = array();


      $data = array(
        'forum_id' => $this->request->getPost('forum_id'),
        'content' => $this->request->getPost('content'),
        'user_id' => $this->session->get('user_id')
        );

      // upload image1
      $thread = new ThreadsReply();
      if(!$thread->save($data)){
        $result['messages'] = "Something went wrong! Please try again later"; 
        $result['status'] = false;
        echo json_encode($result);
        $this->view->disable();       
        exit();
      }

      $reply_info = ThreadsReply::listReplyInfo($thread->id);

      $date = new DateTime($reply_info->date_created,new DateTimeZone("Asia/Hong_Kong"));
      
      $push_data = array(
            'id' => $reply_info->id,
            'forum_id' => $reply_info->forum_id,
            'user_id' => $reply_info->user_id,
            'content' => $reply_info->content,
            'date_created' => $date->format('M d, Y h:i A'),
            'username' => $reply_info->username,
            'profile_pic' => IMAGE_URL_SMALL.$reply_info->profile_pic
        );

       $forum = Threads::findFirstById($data['forum_id']);
        $last_reply = array(
          "username" => $reply_info->username,
          "date_created" => $push_data['date_created']
        );
        $forum->last_reply = json_encode($last_reply);
        @$forum->save();

      $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
      $pusher->trigger('my-channel', 'added-forum_reply', $push_data);
      
      $result['status'] = true;
      $result['data'] = $push_data;
      echo json_encode($result);
      $this->view->disable();
    }
}

